﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
using Microsoft.VisualStudio.Text.Editor;
using System.ComponentModel;

namespace VsSpeakerCompanion.UI
{
    /// <summary>
    /// Interaction logic for Slidebar.xaml
    /// </summary>
    public partial class Sidebar : UserControl
    {
        internal bool IsExpanded
        {
            get;
            private set;
        }

        internal event EventHandler PowerOff;

        /// <summary>
        /// Creates a new instance of <see cref="Sidebar"/>
        /// </summary>
        public Sidebar()
        {
            InitializeComponent();

            this.IsExpanded = true;
        }

        internal void SetContent(UIElement uiElement)
        {
            this.content.Children.Clear();
            this.content.Children.Add(uiElement);
        }

        internal void SetContent(IWidget widget)
        {
            this.SetContent(widget.VisualElement);
        }

        /// <summary>
        /// Handles the MouseEnter
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseEnter(MouseEventArgs e)
        {
            base.OnMouseEnter(e);
            Mouse.OverrideCursor = this.Cursor;
            //this.Opacity = 1;
        }

        /// <summary>
        /// Handles the MouseLeave
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseLeave(MouseEventArgs e)
        {
            base.OnMouseLeave(e);
            Mouse.OverrideCursor = null;
            //this.Opacity = 0.5;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseDown(MouseButtonEventArgs e)
        {
            e.Handled = true;
        }

        private void expandCollapseImageButton_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (IsExpanded)
            {
                Collapse();
            }
            else
            {
                Expand();
            }
        }

        internal void Expand()
        {
            if (!IsExpanded)
            {
                BeginStoryboard((Storyboard)this.FindResource("expandSidebarAnimation"));
                this.expandCollapseImageButton.ToolTip = this.FindResource("collapseSidebarText");
                this.expandCollapseImageButton.Source = this.FindResource("collapseImage") as ImageSource;
                this.sidebarBorder.CornerRadius = new CornerRadius(0);

                IsExpanded = true;
            }
        }

        internal void Collapse()
        {
            if (IsExpanded)
            {
                BeginStoryboard((Storyboard)this.FindResource("collapseSidebarAnimation"));
                this.expandCollapseImageButton.ToolTip = this.FindResource("expandSidebarText");
                this.expandCollapseImageButton.Source = this.FindResource("expandImage") as ImageSource;
                this.sidebarBorder.CornerRadius = new CornerRadius(0, 0, 20, 20);

                IsExpanded = false;
            }
        }

        private void OnCollapseSidebarAnimationCompleted(object sender, EventArgs e)
        {
            this.sidebarBorder.Height = this.header.ActualHeight;
        }

        private void OnExpandSidebarAnimationCompleted(object sender, EventArgs e)
        {
            Storyboard storyboard = (Storyboard)this.FindResource("expandSidebarAnimation");
            DoubleAnimationUsingKeyFrames animation = storyboard.Children[0] as DoubleAnimationUsingKeyFrames;

            this.Height = this.sidebarBorder.Height = animation.KeyFrames[0].Value ;
        }

        private void powerOnOffImageButton_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (PowerOff != null)
            {
                PowerOff(this, new EventArgs());
            }
        }

        internal void UpdatePowerOnOffStatus(bool isPoweredOn)
        {
            this.powerOnOffImageButton.IsEnabled = isPoweredOn;

            if (isPoweredOn)
            {
                this.powerOnOffImageButton.Source = this.FindResource("powerOnImage") as ImageSource;

            }
            else
            {
                this.powerOnOffImageButton.Source = this.FindResource("powerOffImage") as ImageSource;
            }
        }

        internal void SetHeight(double value)
        {
            Storyboard storyboard = (Storyboard)this.FindResource("expandSidebarAnimation");
            DoubleAnimationUsingKeyFrames animation = storyboard.Children[0] as DoubleAnimationUsingKeyFrames;
            animation.KeyFrames[0].Value = value;

            if (this.IsExpanded)
            {
                this.Height = this.sidebarBorder.Height = value;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseMove(MouseEventArgs e)
        {
            e.Handled = true;
        }
    }
}